<template>
	<view class="selectStore">
		<nav-bars title="添加商品"></nav-bars>
		<view class="content">
			<view class="search">
				<view class="searchBox" :class="{'active':searchInput==''}">
					<text class="iconfont icon-search"></text>
					<input type="text" value="" @input="clearSearch" v-model="searchInput" placeholder="搜索商品" confirm-type="search" @confirm="searchFn" />
				</view>
				<view class="cancelBtn" style="flex-shrink: 0;" v-if="searchInput!=''" @click="cancelSearch">取消</view>
			</view>
			<view class="storeList">
				<view class="item" v-for="(item,index) in shopList" :key="index">
					<image :src="item.main_imgurl" mode=""></image>
					<view class="infoBox">
						<view class="shopInfo">
							<view class="shopTit omit-2">
								{{item.product_title}}
							</view>
							<view class="storeName">
								<image :src="storeLogo" mode=""></image>
								{{storeName}}
							</view>
						</view>
						<view class="shopPrice">
							<view class="priceBox">
								￥{{item.price}}
								<text>售价￥{{item.primary_price}}</text>
							</view>
							<view class="plusBtn" @click="addShop(item)">
								<image :src="`${baseImg}/static/adminShop/relePlus.png`" mode=""></image>
								添加
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				baseImg:this.$static,
				searchInput:'',
				store_id:'',
				page:1,
				pagesize:10,
				shopList:[],
				product_id:[],
				storeLogo:'',
				storeName:''
			}
		},
		onLoad(options) {
			this.store_id = options.store_id;
			this.product_id = options.product_id.split(',');
			this.storeLogo = options.storeLogo;
			this.storeName = options.storeName;
			this.getShopList()
		},
		onReachBottom() {
			this.getShopList('onReach')
		},
		methods:{
			// 搜索
			searchFn(){
				this.getShopList()
			},
			
			// 搜索取消按钮
			cancelSearch(){
				this.searchInput = ''
			},
			
			// 获取商品列表
			getShopList(type){
				if(type=='onReach'){
					this.page++
				}else{
					this.page = 1
				}
				let params = {
					page:this.page,
					pagesize:this.pagesize,
					keyword:this.searchInput,
					store_id:this.store_id,
					navStatus:0, //在售商品
				}
				this.$api.getSaleShop(params).then(res=>{
					console.log(res,'在售商品列表')
					if(res.code==1){
						if(type=='onReach'){
							this.shopList = this.shopList.concat(res.data.data)
						}else{
							this.shopList = res.data.data?res.data.data:[]
						}
					}
				})
			},
			
			// 清除搜索
			clearSearch(){
				if(this.searchInput==''){
					this.getShopList()
				}
			},
			
			//商品添加按钮
			addShop(item){
				console.log(this.product_id,'999999999999')
				let productId = item.product_id
				if(this.product_id.findIndex(item1=>item1==productId)!=-1){
					uni.showToast({
						title:'已选择该商品',
						icon:'none'
					})
					return
				}else{
					console.log(item,'选择的商品')
					let pages = getCurrentPages();  //获取所有页面栈实例列表
					console.log(pages)
					let nowPage = pages[ pages.length - 1];  //当前页页面实例
					let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
					console.log(prevPage)
					prevPage.$vm.shopInfo = item;
					uni.navigateBack({
						delta: 1
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	page{height: 100%;}
	.selectStore{
		padding: 32rpx;
		height: 100%;
		background-color: #fff;
		.content{
			.search{
				display: flex;
				align-items: center;
				justify-content: space-between;
				.searchBox{
					display: flex;
					align-items: center;
					transition: 0.2s;
					width: 588rpx;
					height: 80rpx;
					border-radius: 40rpx;
					background-color: #FAFAFA;
					padding: 0 29rpx;
					.iconfont{
						font-size: 28rpx;
						margin-right: 17rpx;
					}
				}
				.active{
					width: 100%;
				}
			}
			.storeList{
				.item{
					display: flex;
					align-items: center;
					image{
						width: 220rpx;
						height: 220rpx;
						border-radius: 10rpx;
						flex-shrink: 0;
						margin-right: 22rpx;
					}
					.infoBox{
						width: 100%;
						display: flex;
						padding: 40rpx 0;
						flex-direction: column;
						border-bottom: 2rpx solid #F5F5F5;
						.shopInfo{
							.shopTit{
								width: 100%;
								font: 500 28rpx/38rpx PingFang SC;
								color: #1B1B1B;
							}
							.storeName{
								display: flex;
								align-items: center;
								margin: 22rpx 0 41rpx 0;
								image{
									width: 28rpx;
									height: 28rpx;
									margin-right: 7rpx;
								}
								font: 500 24rpx/24rpx PingFang SC;
								color: #666666;
							}
						}
						.shopPrice{
							display: flex;
							align-items: flex-end;
							justify-content: space-between;
							.priceBox{
								display: flex;
								flex-direction: column;
								// align-items: flex-end;
								font: 500 36rpx/36rpx PingFang SC;
								color: #E75816;
								text{
									font: 500 22rpx/32rpx PingFang SC;
									color: #999999;
									text-decoration: line-through;
									margin-left: 13rpx;
								}
							}
							.plusBtn{
								display: flex;
								align-items: center;
								image{
									width: 22rpx;
									height: 22rpx;
									margin-right: 10rpx;
								}
								padding: 14rpx 22rpx;
								border: 2rpx solid #B59A57;
								border-radius: 50rpx;
								font: 500 24rpx/24rpx PingFang SC;
								color: #B59A57;
							}
						}
					}
				}
			}
		}
	}
</style>
